<?php

namespace app\common\model\search;

use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UtcDateTime;
use app\common\model\Follow as FollowModel;

trait FollowtaskSearch
{
    //随访计划id
    public function searchTaskIdAttr($query, $value, $data)
    {
        return $query->where('id', $value);
    }

    //随访状态
    public function searchStatusAttr($query, $value, $data)
    {
        return $query->where('status', (int)$value);
    }

    //创建年月
    public function searchCreatedYmdAttr($query, $value, $data)
    {
        $condition = [];
        if (!empty($value))
        {
            $condition[] = ['created_ymd', '>=', new UtcDateTime(strtotime($value[0])*1000)];
            $condition[] = ['created_ymd', '<=',  new UtcDateTime(strtotime($value[1].' 23:59:59')*1000)];
        }
        return $query->where($condition);
    }

    //随访计划名称
    public function searchTasknameAttr($query, $value, $data)
    {
        return $query->whereLike('taskname', trim($value));
    }

    //随访人员
    public function searchFollowStaffAttr($query, $value, $data)
    {
        return $query->where('follow_staff.doctor_code', $value);
    }

    //病案号
    public function searchMedicalRecordsNoAttr($query, $value, $data)
    {
        $task_ids = (new FollowModel())->where('medicalRecordsNo', trim($value))->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //姓名
    public function searchNameAttr($query, $value, $data)
    {
        $task_ids = (new FollowModel())->whereLike('name', trim($value))->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //电话号
    public function searchMobileAttr($query, $value, $data)
    {
        $task_ids = (new FollowModel())->where('presentTelphone', trim($value))->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //1未完成  2已结束  选项
    public function searchIscompleteAttr($query, $value, $data)
    {
        $where = [];
        if ($value == 1) {
            $where[] = ['status', 'in', [0,1]];
        }else{
            $where[] = ['status', 'in', [2,3]];
        }
        return $query->where($where);
    }

    //我的随访-患者姓名
    public function searchMyNameAttr($query, $value, $data)
    {
        $user = request()->user();

        $where[] = ['follow_staff.user_id', '=', $user['id']];
        $where[] = ['name', 'like', '^'.trim($value)];
        $task_ids = (new FollowModel())->where($where)->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //我的随访-手机号
    public function searchMyMobileAttr($query, $value, $data)
    {
        $user = request()->user();

        $where[] = ['follow_staff.user_id', '=', $user['id']];
        $where[] = ['presentTelphone', '=', trim($value)];
        $task_ids = (new FollowModel())->where($where)->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //我的随访-病案号
    public function searchMyMedicalRecordsNoAttr($query, $value, $data)
    {
        $user = request()->user();

        $where[] = ['follow_staff.user_id', '=', $user['id']];
        $where[] = ['medicalRecordsNo', '=', trim($value)];
        $task_ids = (new FollowModel())->where($where)->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //我的随访-随访结果
    public function searchResultAttr($query, $value, $data)
    {
        $user = request()->user();

        $where[] = ['follow_staff.user_id', '=', $user['id']];
        $where[] = ['result', '=', (int)$value];
        $task_ids = (new FollowModel())->where($where)->column('task_id');
        return $query->whereIn('id', $task_ids);
    }

    //多选随访计划名称
    public function searchTasknamesAttr($query, $value, $data)
    {
        return $query->whereIn('id', $value);
    }

    public function searchIdsAttr($query, $value, $data)
    {
        $ids = array_map(function ($id) {
            return new ObjectId($id);
        }, $value);
        return $query->whereIn('_id', $ids);
    }

    //分娩开始日期
    public function searchStartBirthDateAttr($query, $value, $data)
    {
        $task_id = (new FollowModel())->where([
            ['time_of_birth', '>=', $value]
        ])->column('task_id');
        $task_id = array_map(function ($id) {
            return new ObjectId($id);
        }, $task_id);
        return $query->whereIn('id', $task_id);
    }

    //分娩结束日期
    public function searchEndBirthDateAttr($query, $value, $data)
    {
        $task_id = (new FollowModel())->where([
            ['time_of_birth', '<=', $value]
        ])->column('task_id');
        $task_id = array_map(function ($id) {
            return new ObjectId($id);
        }, $task_id);
        return $query->whereIn('id', $task_id);
    }
}